var partnersArr = new Array();
var counter = 0;
function autoComlete(val) {
    if(val.length > 2) {
        $('#autoComplete').load(getPartnersUrl, 'name='+val, function() {
            $('.listPartners ul li').click(function(){
                $('#autoComplete').css({'display':'none'});
                $('#partnerName').val('');
                if(counter < 10) {
                    if($.inArray($(this).attr('class'), partnersArr) == -1) {
                        var delBut = '<img src="/images/icons/deletered.png" class="delSubscrPartner" id="number'+$(this).attr('class')+'" style="cursor:pointer" title="Удалить из списка"/>';
                        $('#subscribedPartners').append('<div>'+$(this).html()+' '+delBut+'</div>')
                        partnersArr[$(this).attr('class')] = $(this).attr('class');
                        $('.delSubscrPartner').click(function(){
                            $(this).parent().empty();
                            partnersArr[$(this).attr('id').replace(/\D+/g,"")] = undefined;
                            if($('.error').css('display') == 'block') {
                                $('.error').css('display', 'none');
                                $('#partnerName').css('background-color', '#FFFFFF');
                            }
                            counter--;
                        });
                        counter++;
                    }
                } else {
                    $('.error').css('display', 'block');
                    $('#partnerName').css('background-color', '#FFE0E1');
                }
            });
        });
        $('#autoComplete').css({'display':'block'});
    } else $('#autoComplete').css({'display':'none'});
}

$(document).ready(
    function() {
        $('#partnerName').keyup(function() {
            autoComlete($(this).val());
        });
        
        $('#submitButton').click(function(){
            $('#workPartners').attr({'value':partnersArr.toString()});
            return true;
        });
    }
);   